SpringBoot小练习

您所在的位置:网站首页 spring bootmybatis根据id查询 SpringBoot小练习

SpringBoot小练习

2023-09-19 21:15| 来源: 网络整理| 查看: 265

实现了用户的登录和注销,登录进去之后就可以对员工的信息进行增删改查了,使用了拦截器来拦截没有登陆的用户进入其他页面。

项目结构

页面

登录页面:

员工管理页面:

新增员工:

修改员工信息:

  数据库信息

用户信息表:

CREATE TABLE `user1` ( `id` int(0) NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; INSERT INTO `user1` VALUES (1, 'root', '123456');

员工信息表:

CREATE TABLE `employee` ( `id` int(0) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `sex` int(0) NOT NULL, `position` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, `department` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic; INSERT INTO `employee` VALUES (1, '小张', 1, '程序媛', '技术部'); INSERT INTO `employee` VALUES (2, '小刘', 0, '部门经理', '技术部'); INSERT INTO `employee` VALUES (3, '老王', 1, '码农', '技术部');   配置

pom.xml:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE com.lyr springboot-demo 0.0.1-SNAPSHOT springboot-demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.1 org.apache.shiro shiro-spring 1.5.2 junit junit 4.12 org.projectlombok lombok mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin

application.yaml:

spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC username: wanide password: "000000" mybatis: type-aliases-package: com.lyr.pojo mapper-locations: classpath:mapper/*.xml

 

实体类

User:

package com.lyr.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String username; private String password; private Employee employee; }

Employee:

package com.lyr.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class Employee { private int id; private String name; private int sex; private String position; //职位 private String department; //部门 }

 

mapper层

UserMapper:

package com.lyr.mapper; import com.lyr.pojo.User; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; @Mapper @Repository public interface UserMapper { //根据用户名获取用户信息 User getUserByName(String username); }

EmployeeMapper:

package com.lyr.mapper; import com.lyr.pojo.Employee; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface EmpMapper { //查询所有员工信息 List getAllEmp(); //新增员工信息 int addEmp(Employee employee); //删除员工 int deleteEmp(String name); //更新员工信息 int updateEmp(Employee employee); //根据员工姓名查询员工信息 Employee getEmpByName(String name); }

UserMapper.xml:

select * from user1 where username=#{username}

EmployeeMapper.xml:

select * from employee insert into employee (name,sex,position,department) values (#{name},#{sex},#{position},#{department}) delete from employee where name=#{name} update employee set name=#{name},sex=#{sex},position=#{position},department=#{department} where id=#{id} select * from employee where name=#{name}

 

Controller层

UserController类:

package com.lyr.controller; import com.lyr.pojo.Employee; import com.lyr.pojo.User; import com.lyr.service.EmpService; import com.lyr.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; import java.util.List; @Controller public class UserController { @Autowired private UserService userService; @Autowired private EmpService empService; @RequestMapping({"/","/index"}) public String toLogin(){ return "index"; } @RequestMapping("/login") public String login(String username, String password, Model model, HttpSession session){ User user = userService.getUserByName(username); //用户存在时 if(user!=null){ //用户名密码都正确时 if (password.equals(user.getPassword())){ List allEmp = empService.getAllEmp(); session.setAttribute("userInfo",username); return "redirect:/showAll"; }else { //密码不正确时 model.addAttribute("msg","用户名或者密码错误"); return "index"; } }else { model.addAttribute("msg","用户名或者密码错误"); return "index"; } } @RequestMapping("/logout") public String logout(HttpSession session){ //注销用户 session.invalidate(); return "index"; } }

EmpController:

package com.lyr.controller; import com.lyr.pojo.Employee; import com.lyr.service.EmpService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; import java.util.List; @Controller public class EmpController { @Autowired private EmpService empService; //查询员工 @RequestMapping("/queryEmp") public String showEmployee(String name, Model model){ if(!name.equals("")){ Employee emp = empService.getEmpByName(name); model.addAttribute("emp",emp); return "show"; }else { //输入为空时显示所有用户 return "redirect:/showAll"; } } //查询所有用户 @RequestMapping("/showAll") public String showAll(Model model){ List allEmp = empService.getAllEmp(); model.addAttribute("emp",allEmp); return "show"; } //新增用户信息 @RequestMapping("/toAddEmp") public String toAddEmp(){ return "addEmp"; } @RequestMapping("/addEmp") public String addEmp(Employee employee){ empService.addEmp(employee); return "show"; } //跳转到修改用户信息页面 @RequestMapping("/toUpdate/{name}") public String toUpdate(@PathVariable("name")String name,Model model){ Employee emp = empService.getEmpByName(name); model.addAttribute("emp",emp); return "updateEmp"; } //修改员工信息 @RequestMapping("/updateEmp") public String updateEmp(Employee employee,Model model){ empService.updateEmp(employee); return "show"; } //删除用户信息 @RequestMapping("/deleteEmp/{name}") public String deleteEmp(@PathVariable("name")String name){ empService.deleteEmp(name); return "show"; } }

 

拦截器

自定义拦截器userInterceptor:

package com.lyr.config; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class userInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Object userInfo = request.getSession().getAttribute("userInfo"); //没有session信息,也就是没登陆,不放行 if(userInfo == null){ request.setAttribute("msg","没有权限,请先登录"); request.getRequestDispatcher("/").forward(request,response); return false; } return true; } }

myMvcConfig类:

package com.lyr.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class myMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //拦截所有页面除了"/index","/","/login","/css/**","/img/**" registry.addInterceptor(new userInterceptor()).addPathPatterns("/**") .excludePathPatterns("/index","/","/login","/css/**","/img/**"); } }

 

前端html和service层可以去GitHub查看完整代码。

GitHub地址:https://github.com/wanide/SpringBoot-Demo



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3